﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using SimpleTaskManager.Domain.Abstract;

namespace SimpleTaskManager.Domain.Concrete
{
    /// <summary>
    /// A class that can be used to encrypt passwords using the SHA1 algorithm
    /// </summary>
    public class SHA1PasswordEncrypter : IPasswordEncrypter
    {
        /// <summary>
        /// Creates the SHA1 hash of a given string.
        /// </summary>
        /// <param name="password">The string to hash.</param>
        /// <returns>The SHA1 hashed string.</returns>
        public string HashPassword(string password)
        {
            SHA1Managed hashString = new SHA1Managed();

            //creating a byte array from the password string
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] pass = UE.GetBytes(password);

            //computing the SHA1 hash 
            byte[] hashValue = hashString.ComputeHash(pass);

            //writing the byte array to a hexadecimal string
            string hex = string.Empty;
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }

            return hex;
        }
    }
}
